<?php

namespace addons\mental\model;

use think\Model;

class ExtensionStatistics extends Model
{

    // 表名
    protected $name = 'mental_extension_statistics';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];




    /**
     * 统计数据
     * $mainId      量表id
     * $type        类型:1=点击数,2=提交数,3=拉起付款数,4=付款成功数
     * $extensionId 分销id
     */
    public static function setIncNum($type, $extensionId = 0)
    {
        if ($extensionId !== 0) {
            $me = \think\Db::name('mental_extension')->where(['id' => $extensionId])->find();
            if ($me) {
                $mes = self::where(['user_id' => $me['user_id'], 'type' => $type])->whereTime('createtime', 'today')->find();
                if ($mes) {
                    $mes->setInc('num');
                } else {
                    self::create([
                        'user_id' => $me['user_id'],
                        'type' => $type,
                        'num' => 1,
                        'createtime' => time(),
                    ]);
                }
            }
        }

        return true;
    }



    public static function setDecNum($type, $extensionId = 0)
    {
        if ($extensionId !== 0) {
            $me = \think\Db::name('mental_extension')->where(['id' => $extensionId])->find();
            if ($me) {
                $mes = self::where(['user_id' => $me['user_id'], 'type' => $type])->whereTime('createtime', 'today')->find();
                if ($mes) {
                    $mes->setInc('num');
                }
            }
        }

        return true;
    }
}
